package com.jy.Dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tomcat.dbcp.dbcp.PStmtKey;

import com.jy.Dao.StudentDao;
import com.jy.entity.Student;
import com.jy.uti.AliPool;

public class StudentDaoimpl implements StudentDao {

	@Override
	public List<Student> find() throws SQLException {
		List<Student> list=new ArrayList<>();
		Connection conn=AliPool.getconn();
		String sql="select * from student;";
		PreparedStatement pst=conn.prepareStatement(sql);
		ResultSet rs=pst.executeQuery();
		while(rs.next()){
			String name=rs.getString(2);
			int age=rs.getInt(3);
			Student student=new Student(name,age);
			list.add(student);
		}
		conn.close();
		return list;
	}

	
	@Override
	public int add(Student a) throws SQLException {
		String sql="insert into student VALUES(null,?,?);";
		Connection conn=AliPool.getconn();
		int num=0;
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1, a.getName());
			pst.setInt(2, a.getAge());
			num=pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		conn.close();
		return num;
	}

}
